/* Author:  Michael Annichiarico (mike.gcdeveloper@gmail.com)
 * Details: signver was developed in support of the Parallel
 * Elliptic Curve Cryptography Engine for OpenSSL
 * ====================================================================
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <math.h>
#include "apps/apps.h"
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/objects.h>

#include<openssl/ecdsa.h>
#include<openssl/ec.h>
#include<crypto/ec/ec_lcl.h>

#define myerr printf("Error at Line: %d\n",__LINE__);

#define PROG signver_prog

void doall();
int getoptions(int argc, char** argv);

char digest[20];

typedef struct options_st {
   char* engine;

} options_t;

options_t options;

int MAIN(int argc,char** argv) {
    int i;
    for(i=0;i<20;++i) digest[i]=i;

    if(getoptions(argc,argv)) {
        printf("Failed Parsing Options -- ABORT\n");
        return(-2);
    }

    if(options.engine) {
        if(generic_load_engine_fn(options.engine,NULL,0,NULL,0)==0) {
            printf("Load Engine '%s' FAILED - ABORT\n",options.engine);
            return(-1);
        }
    }

    doall();
}

int getoptions(int argc, char** argv) {
       int aflag = 0;
       int bflag = 0;
       char *cvalue = NULL;
       int index;
       int c;

        memset((void*)&options,sizeof(options),0);
     
       opterr = 0;
     
       while ((c = getopt (argc, argv, "abc:e:")) != -1)
         switch (c)
           {
           case 'a':
             aflag = 1;
             break;
           case 'b':
             bflag = 1;
             break;
           case 'c':
             cvalue = optarg;
             break;
           case 'e':
             options.engine = optarg;
             break;
           case '?':
             if (optopt == 'c')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (isprint (optopt))
               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             else
               fprintf (stderr,
                        "Unknown option character `\\x%x'.\n",
                        optopt);
             return 1;
           default:
             abort ();
           }
     
//       printf ("aflag = %d, bflag = %d, cvalue = %s\n",
 //              aflag, bflag, cvalue);
     
       for (index = optind; index < argc; index++)
         printf ("Non-option argument %s\n", argv[index]);
       return 0;
     }


void doall() { 

 int        ret;
 ECDSA_SIG *sig;
 EC_KEY    *eckey = EC_KEY_new();
// EC_KEY    *key = eckey;
 if (eckey == NULL)
        {
        /* error */myerr;
        }
 eckey->group = EC_GROUP_new_by_curve_name(NID_sect233r1);
 if (eckey->group == NULL)
        {
        /* error */myerr;
        }
 if (!EC_KEY_generate_key(eckey))
        {
        /* error */myerr;
        }

//Second step: compute the ECDSA signature of a SHA-1 hash value using ECDSA_do_sign

 sig = ECDSA_do_sign(digest, 20, eckey);
 if (sig == NULL)
        {
        /* error */
        /* error */myerr;
        }

/*or using ECDSA_sign

 unsigned char *buffer, *pp;
 int            buf_len;
 buf_len = ECDSA_size(eckey);
 buffer  = OPENSSL_malloc(buf_len);
 pp = buffer;
 if (!ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey);
        {
//        /* error 
        }
*/
//Third step: verify the created ECDSA signature using ECDSA_do_verify

 ret = ECDSA_do_verify(digest, 20, sig, eckey);

//or using ECDSA_verify

// ret = ECDSA_verify(0, digest, 20, buffer, buf_len, eckey);

//and finally evaluate the return value:

 if (ret == -1)
        {
        /* error */
        /* error */myerr;
        }
 else if (ret == 0)
        {
        /* incorrect signature */
        printf("incorrect sig\n");
        }
 else   /* ret == 1 */
        {
        /* signature ok */
        printf("CORRECT SIG!\n");
        }
//printf("Signing Again! hopefully no segfault\n");
 //sig = ECDSA_do_sign(digest, 20, eckey);
}
